﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Customers : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGridView();
            }
        }

        private void LoadGridView()
        {
            var customers = CustomerBLL.GetAllCustomers().OrderByDescending(c => c.CustomerId).ToList();
            Session["Customers"] = customers;
            gvCustomers.DataSource = customers;
            gvCustomers.DataBind();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            var telephone = tbTelephone.Text.Trim();

            if (string.IsNullOrWhiteSpace(telephone))
            {
                LoadGridView();
            }
            else
            {
                var customer = CustomerBLL.GetCustomerDetails(telephone);

                gvCustomers.DataSource = customer == null ? null : new List<Customer> {customer};
                gvCustomers.DataBind();
            }
        }

        protected void gvCustomers_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomers.PageIndex = e.NewPageIndex;
            LoadGridView();
        }

        protected void ibUnban_OnClick(object sender, ImageClickEventArgs e)
        {
            UserBLL.ChangeUserStatus(Request.QueryString["username"], true);
            gvCustomers.DataSource = Session["Customers"];
            gvCustomers.DataBind();
        }

        protected void ibBan_OnClick(object sender, ImageClickEventArgs e)
        {
            UserBLL.ChangeUserStatus(Request.QueryString["username"], false);
            gvCustomers.DataSource = Session["Customers"];
            gvCustomers.DataBind();
        }
    }
}